import { TODO_ADD, TODO_CHENCKEDAll, TODO_CLEAR, TODO_DEL, TODO_TYPE, TODO_UPDATEBYID } from "../action/actionType";

/*
  学习目标：Redux设计-Reducer的拆分与合并
  步骤：
     1. 拆分reducer - 按功能模块的名字去定义reducer文件
     2. 合并
     3. 使用rootReducer创建store
     4. 分模块取出state
*/
const initState = {
    list: [
        {
            id: 1,
            task: '学习React',
            isDone: false,
        },
        {
            id: 2,
            task: '学习Vue',
            isDone: true,
        },
        {
            id: 3,
            task: '学习JS',
            isDone: false,
        },
    ],
    type: 'all',
};
export default function todoReducer(state = initState, action) {
    const { type, payload } = action;
    switch (type) {
        case TODO_DEL:
            return {
                ...state,
                list: state.list.filter(item => item.id !== payload)
            }
        case TODO_UPDATEBYID:
            return {
                ...state,
                list: state.list.map(item => ({ ...item, isDone: item.id === payload ? !item.isDone : item.isDone }))
            }
        case TODO_CLEAR:
            return {
                ...state,
                list: state.list.filter(item => item.isDone !== true)
            }
        case TODO_ADD:
            return {
                ...state,
                list: [{ id: Date.now(), isDone: false, task: payload }, ...state.list]
            }
        case TODO_TYPE:
            return {
                ...state,
                type: payload
            }
        case TODO_CHENCKEDAll:
            return {
                ...state,
                list: state.list.map(item => ({ ...item, isDone: payload }))
            }

        // 💥 一定要有默认返回值
        default:
            return state;
    }

    // 默认返回state
}